<template>
  <!-- <p>
    鼠标获取的x坐标：{{ elementX }} ,y轴坐标：{{ elementY }},鼠标在外面：{{
      isOutside
    }}
  </p> -->
  <div class="goods-image">
    <!-- 放大镜图片 -->
    <div
      class="large"
      v-show="show"
      :style="[
        {
          backgroundImage: `url(${list[curIndex]})`,
          backgroundPosition: `${bg.x}px ${bg.y}px`,
        },
      ]"
    ></div>
    <div class="middle" ref="target">
      <img :src="list[curIndex]" alt="" />
      <!-- 蒙层容器 -->
      <div
        v-show="show"
        class="layer"
        :style="{ left: `${pos.left}px`, top: `${pos.top}px` }"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(img, i) in list"
        :key="i"
        :class="{ active: curIndex === i }"
        @mouseenter="curIndex = i"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 显示与隐藏
    const show = ref(false)
    // 获取索引
    const curIndex = ref(0)
    const target = ref(null)
    // （elementX, elementY）：鼠标在盒子移动的坐标值, isOutside:鼠标移动是否在外面
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    const pos = ref({ left: 0, top: 0 })
    watch([elementX, elementY], () => {
      // 横坐标
      if (elementX.value < 100) {
        pos.value.left = 0
      } else if (elementX.value > 300) {
        pos.value.left = 200
      } else {
        pos.value.left = elementX.value - 100
      }
      // 纵坐标
      if (elementY.value < 100) {
        pos.value.top = 0
      } else if (elementY.value > 300) {
        pos.value.top = 200
      } else {
        pos.value.top = elementY.value - 100
      }
      // 计算背景图片放大
      bg.value.x = -pos.value.left * 2
      bg.value.y = -pos.value.top * 2
      if (isOutside.value === true) {
        show.value = false
      } else {
        show.value = true
      }
    })
    // 右侧图片放大
    const bg = ref({ x: 0, y: 0 })
    return { show, bg, pos, curIndex, target, elementX, elementY, isOutside }
  }
}
// 放大镜实现：
// 前置知识：浏览器坐标系从左上角开始（x:0,y:0）
// 1.获取元素内鼠标移动的坐标
// 2.监听元素内鼠标移动的坐标变化=》控制蒙层移动=》计算蒙层left:0,top:0
// 结论：
// 1.蒙层移动的范围：100<pos<300
// 2.蒙层位置计算公式：
// left=elementX-100|top=elementY-100

</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
